//
//  AppDelegate.m
//  17--SandBox
//
//  Created by 冀永金 on 16/8/5.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "AppDelegate.h"
#import "Book.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 沙盒 （SandBox）
    // Documents(文件文档，用户数据存储)
    // Library(资源，一般用来存放，程序员要储存的数据)
        //Caches (缓存文件)
        // Preferences (用户信息，用户设置，NSUserDefaults)
    
    // tmp(临时目录，下载的临时文件一般放在这里)
    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"isLogin"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    // 2.获取沙盒路径
    // 快捷获取到对应目录的C函数
    // 根目录
  //  NSHomeDirectory()
    NSLog(@"-----------%@",NSHomeDirectory());
    //临时目录
    //NSTemporaryDirectory()
    NSLog(@"%@",NSTemporaryDirectory());
      //参数1：搜索文件夹路径    参数2：在用户作用域下搜索  // 参数3：YES  or NO   YES代表绝对路径（一般都用这个）   NO代表相对路径
    NSArray *pathArr =   NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"++++++++++%@",[pathArr firstObject]);
    
    
    // NSBudle .app文件包
    NSLog(@"%@\n\n\n\n\n",[NSBundle mainBundle]);
    
    
    // 简单的文件读写 Input  Output
    NSString *helloStr = @"Hello,I/O File";
    // 一般拼接路径时  我们用这个stringByAppendingPathComponent
    //NSString *writePath = [[pathArr firstObject] stringByAppendingString:@"/Hello.txt"];
  NSString *writePath =   [[pathArr firstObject] stringByAppendingPathComponent:@"Hello.txt"];
    NSError *error = nil;
 BOOL isSuess = [helloStr writeToFile:writePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        NSLog(@"存储失败");
    }else {
        NSLog(@"存储成功");
    }
    // 2>读取路径对应的文字
    NSError *readError = nil;
   NSString *readStr =  [NSString stringWithContentsOfFile:writePath encoding:NSUTF8StringEncoding error:&readError];
    NSLog(@"%@",readStr);
    
    
    
    // 3>将数组写入文件
    NSArray *arr = @[@"你",@"是",@"傻",@"逼"];
    NSString *arrayPath = [[pathArr firstObject] stringByAppendingPathComponent:@"arr.plist"];
 BOOL isArrayWrite = [arr writeToFile:arrayPath atomically:YES];
    
    if (isArrayWrite) {
        NSLog(@"成功写入");
    }else {
        NSLog(@"写入失败");
    }
    
    // 4将数组读取
    NSArray *readArr = [NSArray arrayWithContentsOfFile:arrayPath];
    NSLog(@"%@",readArr);
    
    // 5将字典存入文件
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"小明",@"name",@"16",@"age", nil];
    NSString *dicPath = [[pathArr firstObject]stringByAppendingPathComponent:@"dic.plist"];
  BOOL isWriteSucess =  [dic writeToFile:dicPath atomically:YES];
    if (isWriteSucess) {
        NSLog(@"字典写入成功");
    }else {
        NSLog(@"字典写入失败");
    }
    // 6将字典从文件读出
    NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
    NSLog(@"%@",readDic);
    // 7> 将Data 类型写入本地
    UIImage *image = [UIImage imageNamed:@"1"];
    NSData *imagedata = UIImageJPEGRepresentation(image, 0.1);
    NSString *dataPath = [[pathArr firstObject] stringByAppendingPathComponent:@"imageData"];
  BOOL isDataWriteSuccess =  [imagedata writeToFile:dataPath atomically:YES];
    
    if (isDataWriteSuccess) {
        NSLog(@"图片成功写入");
    }
    else {
        NSLog(@"图片写入失败");
    }
    
    //8将data类型 取出
    NSData *imageNewData = [NSData dataWithContentsOfFile:dataPath];
    UIImage *fileImage = [UIImage imageWithData:imageNewData];
    NSLog(@"%@",fileImage);
    
    // 二。复杂对象文件读写，自定义类型
    // 归档/返归档，序列化/反序列化
    // 归档 将对象存储到本地
    Book *book = [Book new];
    book.bookName = @"乌龙院";
    book.bookPrice = @"49";
    book.bookAuthor = @"未知";
    // 归档
    NSString *bookPath = [[pathArr firstObject] stringByAppendingPathComponent:@"book.plist"];
   BOOL isBookWriteSucdess =  [NSKeyedArchiver archiveRootObject:book toFile:bookPath];
    if (isBookWriteSucdess) {
        NSLog(@"写入成功");
    }else {
        NSLog(@"失败");
    }
    
    // 放归档
   Book *book1 = [NSKeyedUnarchiver unarchiveObjectWithFile:bookPath];
    NSLog(@"%@",book1.bookName);
    
    
    // 如果对象要实现反归档和归档
    // 1.对象需要签订 <NSCoding>
    // 2.实现协议方法
    //initWithCoder:(NSCoder *)aDecoder反归档用
    // encodingWithCoder 归档用
    
    //3.归档 使用NSKeyedArchiver
    // 4.反归档NSKeyedUnarchiver 
    
    
    // Override point for customization after application launch.
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
